﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WidgetsDesign.API.DataBase;
using WidgetsDesign.API.Models;

namespace WidgetsDesign.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DisplayBoardController : Controller
    {
        private readonly AppDbContext _context;
        public DisplayBoardController(AppDbContext context)
        {
            _context = context;
        }
        [HttpGet]
        public async Task<IActionResult> GetOption()
        {
            var query = from d in _context.displayboard where d.Id == 1 select d;
            DisplayBoardModel result = await query.FirstOrDefaultAsync();
            return Ok(new { ret = true, data = result, code = 200 });
        }
        [HttpPost]
        public async Task<IActionResult> SetOption(DisplayBoardModel displayBoard)
        {
            var query = from d in _context.displayboard where d.Id == 1 select d;
            DisplayBoardModel result = await query.FirstOrDefaultAsync();
            if (displayBoard.RunningDays != null)
            {
                result.RunningDays = displayBoard.RunningDays;
            }
            if (displayBoard.DayTitle != null)
            {
                result.DayTitle = displayBoard.DayTitle;
            }
            if (displayBoard.Aircrew != null)
            {
                result.Aircrew = displayBoard.Aircrew;
            }
            if (displayBoard.Emergency != null)
            {
                result.Emergency = displayBoard.Emergency;
            }
            if (displayBoard.Mien != null)
            {
                result.Mien = displayBoard.Mien;
            }
            if (displayBoard.LaborUnion != null)
            {
                result.LaborUnion = displayBoard.LaborUnion;
            }
            if(displayBoard.Introduce != null)
            {
                result.Introduce = displayBoard.Introduce;
            }
            if(displayBoard.WelcomeTitle != null)
            {
                result.WelcomeTitle = displayBoard.WelcomeTitle;
            }
            await _context.SaveChangesAsync();
            return Ok(new { ret = true, data = result, code = 200 });
        }
    }
}
